package com.itheima.admin.listener;

import com.itheima.admin.service.WemediaNewsAutoScanService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@Slf4j
public class WemediaNewsAutoScanListener {

    @Autowired
    private WemediaNewsAutoScanService wemediaNewsAutoScanService;

    @KafkaListener(topics = BusinessConstants.MqConstants.WM_NEWS_DOWN_OR_UP_TOPIC)
    public void autoScanWemediaNews(ConsumerRecord<String,String> msgRecord){

        //开始，唯一id，做这次业务的表示。好追踪
        String uuid = UUID.randomUUID().toString();
        String wmNewsId = msgRecord.value();
        String topic = msgRecord.topic();
        int partition = msgRecord.partition();
        long offset = msgRecord.offset();
        log.info("自动审核开始：uuid={},id={},t={},p={},os={}", uuid, wmNewsId, topic, partition, offset);
        //审核
        try {
            wemediaNewsAutoScanService.autoScanWemediaNewsById(Long.valueOf(wmNewsId),uuid);
        } catch (Exception e) {
            log.error("【出错了】自动审核失败：uuid={},id={},t={},p={},os={}", uuid, wmNewsId, topic, partition, offset,e);
        }
        //结束
        log.info("自动审核结束：uuid={},id={},t={},p={},os={}", uuid, wmNewsId, topic, partition, offset);
    }
}
